﻿<%@ WebHandler Language="C#" Class="MemberLogin" %>

using System;
using System.Web;
using System.Text;
using System.Data;
using System.Collections.Generic;
using System.Web.SessionState;
public class MemberLogin : IHttpHandler, IRequiresSessionState
{
    public bool IsReusable
    {
        get
        {
            return true;
        }
    }
    public void ProcessRequest(HttpContext context)
    {
        try
        {
            string operType = ReadRequest.RequestString("t");

            switch (operType)
            {
                case "setAppointtype":
                    setAppointtype();
                    break;
                case "MailCheck":
                    MailCheck();
                    break;
                case "NicknameCheck":
                    NicknameCheck();
                    break;
                case "add":
                    Add();
                    break;
                case "addMobile":
                    addMobile();
                    break;
                case "login_t":
                    userlogin();
                    break;
                case "login_out":
                    userloginout();
                    break;
                case "find_pws_s":
                    find__pws();
                    break;
                case "find_pws_mobel":
                    find_pws_mobel();
                    break;
                case "s_admin_login":
                    ht_admin_login();
                    break;
                case "s_admin_login_out":
                    admin_loginout();
                    break;
                case "savemsg":
                    savemsg();
                    break;
                default:
                    break;
            }
        }
        catch
        {
            alertmessageAndGoback("操作异常");
        }
    }
    private void setAppointtype()
    {
        int ID = ReadRequest.RequestInt("ii");
        int State = ReadRequest.RequestInt("sa");
        // Model.Documentary model = BLL.Documentary.GetModel(ID);
        Model.LotteryChippedUser model = BLL.LotteryChippedUser.GetModel(ID);
        if (model.id > 0)
        {
            model.Appointtype = State;
            BLL.LotteryChippedUser.Update(model);
            returnmessage(true); return;
        }
        else
        {
            returnmessage(false); return;
        }
    }
    private void savemsg()
    {
        string re = ReadRequest.RequestHtmlEnCode("re");
        string txtcont = ReadRequest.RequestHtmlEnCode("txtcont");
        long UserID = ReadRequest.RequestLong("user_id");
        if (string.IsNullOrEmpty(re) && string.IsNullOrEmpty(txtcont))
        {
            returnmessage(false);
            return;
        }
        Model.SF_Message savemsg = new Model.SF_Message();
        savemsg.m_cont = txtcont;
        savemsg.sendID = Convert.ToInt16(re);
        savemsg.UserID = UserID;
        savemsg.sendTime = DateTime.Now;
        savemsg.i_type = 2;
        if (BLL.SF_Message.Add(savemsg) > 0)
        {
            alertmessageAndGoback("保存成功!");
        }
    }
    public void MailCheck()
    {
        string email = ReadRequest.RequestHtmlEnCode("txtemail");
        if (string.IsNullOrEmpty(email))
        {
            returnmessage(false);
            return;
        }
        email = email.Trim();
        if (email.Length < 3 || email.Length > 30)
        {
            returnmessage(false);
            return;
        }
        if (BLL.CMember.GetIsExitemail(email))
        {
            returnmessage(false);
            return;
        }
        returnmessage(true);
    }
    public void NicknameCheck()
    {
        string txtnickname = ReadRequest.RequestHtmlEnCode("txtnickname");
        if (string.IsNullOrEmpty(txtnickname))
        {
            returnmessage(false);
            return;
        }
        txtnickname = txtnickname.Trim();
        if (txtnickname.Length < 3 || txtnickname.Length > 30)
        {
            returnmessage(false);
            return;
        }
        if (BLL.CMember.GetOneBynickname(txtnickname) != null)
        {
            returnmessage(false);
            return;
        }
        returnmessage(true);
    }
    private void Add()
    {
        string username = ReadRequest.RequestString("username");
        string password = ReadRequest.RequestString("userpass");
        string email = ReadRequest.RequestString("email");
        string checkcode = ReadRequest.RequestString("checkcode");
        string trendsCode = "";
        try
        {
            trendsCode = System.Web.HttpContext.Current.Session[NtHelp.regitCode].ToString();
        }
        catch { }
        if (string.IsNullOrEmpty(trendsCode) || string.IsNullOrEmpty(checkcode) || trendsCode.ToUpper() != checkcode.ToUpper())
        {
            returnalert("动态验证码错误，请重试！");
            return;
        }
        if (!string.IsNullOrEmpty(username))
        {
            username = username.Trim();
        }
        else
        {
            returnalert("用户名不正确!");
            return;
        }
        if (string.IsNullOrEmpty(username) || username.Length < 2 || username.Length > 16)
        {
            returnalert("用户名不正确!");
            return;
        }
        if (!R_nameCheck(username))
        {
            returnalert("用户名已被占用!");
            return;
        }
        if (string.IsNullOrEmpty(password))
        {
            returnalert("密码不正确!");
            return;
        }
        if (string.IsNullOrEmpty(password) || password.Length < 6 || password.Length > 20)
        {
            returnalert("密码不正确!");
            return;
        }
        Model.CMember model = new Model.CMember();
        model.nickname = username;
        model.mob = "";
        model.u_password = BLL.Security.MD5(password);
        model.rnd = BLL.CMember.getRnd();
        model.registerTime = DateTime.Now;
        model.cgroup = 0;
        model.uesrState = 1;
        model.userLevel = 3;//注册就是银卡会员等级：1普通会员，2测试会员，3.银卡；4.金卡；5.钻石；6.一卡通（最高级会员）
        model.lastLoginTime = DateTime.Now;
        model.registerIP = ReadRequest.getUserRealIP();
        model.email = email;

        model.realb = null;
        model.realnameb = false;
        model.cardidb = false;
        model.mobb = false;
        model.emailb = false;
        model.zhifuPassword = BLL.Security.MD5("123456");
        model.mibaoPassword = BLL.Security.MD5("123456");
        long id = BLL.CMember.Add(model);
        if (id > 0)
        {
            BLL.Session.Add(BLL.CMember.LoginUserSessionKey, BLL.CMember.GetModel(id));

            Model.UsersAccount userAccount = new Model.UsersAccount();
            userAccount = BLL.UsersAccount.GetModelByuser_id(model.Id);
            if (userAccount != null)
            {
                BLL.UsersAccount.Delete(userAccount.Id);
            }
            Model.CompanyIntr codeModel = BLL.CompanyIntr.GetModel(17);//赠送积分数
            Model.CompanyIntr goldModel = BLL.CompanyIntr.GetModel(16);//赠送金币数
            userAccount = new Model.UsersAccount();
            userAccount.user_id = id;
            userAccount.total = 0;
            userAccount.addtime = DateTime.Now;
            if (codeModel.Content == null)
            {
                userAccount.integralTotal = 0;
            }
            else
            {
                userAccount.integralTotal = decimal.Parse(codeModel.Content.Trim());
            }
            if (goldModel.Content == null)
            {
                userAccount.goldTotal = 0;
            }
            else
            {
                userAccount.goldTotal = decimal.Parse(goldModel.Content.Trim());
            }
            BLL.UsersAccount.Add(userAccount);
            returnalert("注册成功！");
            return;
        }
        else
        {
            returnalert("注册失败，请重试!");
            return;
        }
    }
    private void addMobile()
    {
        string checkcodemobile = ReadRequest.RequestString("checkcode2");//动态验证码
        string mobile = ReadRequest.RequestString("mobile");
        string mobileusername = ReadRequest.RequestString("mobileusername");
        string checkcode2 = ReadRequest.RequestString("checkcodemobile");//手机验证码
        string mobilepass = ReadRequest.RequestString("mobilepass");
        string trendsCode = "";//缓存动态验证码
        string mobileCode = "";//手机数据库验证码
        try
        {
            trendsCode = System.Web.HttpContext.Current.Session[NtHelp.regitCode].ToString();
        }
        catch { }
        if (string.IsNullOrEmpty(trendsCode) || string.IsNullOrEmpty(checkcode2) || trendsCode.ToUpper() != checkcode2.ToUpper())
        {
            //alertmessageAndGoback("动态验证码错误，请重试！");
            returnalert("动态验证码错误，请重试！");
            return;
        }
        else
        {
            Model.MsgSendInfo msgInfo = BLL.MsgSendInfo.GetModelOrderByAddtime(1, mobile);
            if (msgInfo == null)
            {
                returnalert("您的电话号码有误！");
                return;
            }
            else
            {
                mobileCode = msgInfo.code;
                //int effMinutes = (DateTime.Now - msgInfo.addTime).Minutes;
                //if (effMinutes < 1)
                //{
                //    mobileCode = msgInfo.code;
                //}
            }
            if (string.IsNullOrEmpty(checkcodemobile) || string.IsNullOrEmpty(mobileCode) || checkcodemobile.ToUpper() != mobileCode.ToUpper())
            {
                returnalert("您输入的验证码失效，请重新发送短信！");
                return;
            }

            if (!string.IsNullOrEmpty(mobileusername))
            {
                mobileusername = mobileusername.Trim();
            }
            else
            {
                returnalert("用户名不正确!");
                return;
            }
            if (string.IsNullOrEmpty(mobileusername) || mobileusername.Length < 2 || mobileusername.Length > 16)
            {
                returnalert("用户名不正确!");
                return;
            }
            if (!R_nameCheck(mobileusername))
            {
                returnalert("用户名已被占用!");
                return;
            }
            if (string.IsNullOrEmpty(mobilepass))
            {
                returnalert("密码不正确!");
                return;
            }
            if (string.IsNullOrEmpty(mobilepass) || mobilepass.Length < 6 || mobilepass.Length > 20)
            {
                returnalert("密码不正确!");
                return;
            }
            Model.CMember model = new Model.CMember();
            model.nickname = mobileusername;
            model.mob = mobile;
            model.u_password = BLL.Security.MD5(mobilepass);
            model.rnd = BLL.CMember.getRnd();
            model.registerTime = DateTime.Now;
            model.cgroup = 0;
            model.uesrState = 1;
            model.userLevel = 3;
            model.lastLoginTime = DateTime.Now;
            model.registerIP = ReadRequest.getUserRealIP();
            model.email = "";

            model.realb = null;
            model.realnameb = false;
            model.cardidb = false;
            model.mobb = false;
            model.emailb = false;
            model.zhifuPassword = BLL.Security.MD5("123456");
            model.mibaoPassword = BLL.Security.MD5("123456");

            long id = BLL.CMember.Add(model);
            if (id > 0)
            {
                BLL.Session.Add(BLL.CMember.LoginUserSessionKey, BLL.CMember.GetModel(id));

                Model.UsersAccount userAccount = new Model.UsersAccount();
                userAccount = BLL.UsersAccount.GetModelByuser_id(model.Id);
                if (userAccount != null)
                {
                    BLL.UsersAccount.Delete(userAccount.Id);
                }
                Model.CompanyIntr codeModel = BLL.CompanyIntr.GetModel(17);//赠送积分数
                Model.CompanyIntr goldModel = BLL.CompanyIntr.GetModel(16);//赠送金币数
                userAccount = new Model.UsersAccount();
                userAccount.user_id = id;
                userAccount.total = 0;
                userAccount.addtime = DateTime.Now;
                if (codeModel.Content == null)
                {
                    userAccount.integralTotal = 0;
                }
                else
                {
                    userAccount.integralTotal = decimal.Parse(codeModel.Content.Trim());
                }
                if (goldModel.Content == null)
                {
                    userAccount.goldTotal = 0;
                }
                else
                {
                    userAccount.goldTotal = decimal.Parse(goldModel.Content.Trim());
                }

                BLL.UsersAccount.Add(userAccount);
                returnalert("注册成功！");
                return;
            }
            else
            {
                returnalert("注册失败，请重试!");
                return;
            }
        }
    }
    public bool R_nameCheck(string name)
    {
        if (string.IsNullOrEmpty(name))
        {
            return false;
        }
        name = name.Trim();
        if (name.Length < 2 || name.Length > 16)
        {
            return false;
        }
        if (BLL.CMember.GetIsExitNickname(name))
        {
            return false;
        }
        return true;
    }

    public bool R_name_had(string name)
    {
        if (string.IsNullOrEmpty(name))
        {
            return false;
        }
        name = name.Trim();
        if (name.Length < 2 || name.Length > 16)
        {
            return false;
        }
        if (BLL.CMember.GetIsExitNickname(name))
        {
            return true;
        }
        return false;
    }
    public bool R_mobCheck(string mob)
    {
        if (string.IsNullOrEmpty(mob))
        {
            return false;
        }
        mob = mob.Trim();
        if (mob.Length != 11)
        {
            return false;
        }
        if ("11".Equals(mob.Substring(0, 2)))
        {
            return false;
        }
        if (BLL.CMember.GetIsExitmob(mob))
        {
            return false;
        }
        return true;
    }

    private void userlogin()
    {
        string us = ReadRequest.RequestString("us");
        string up = ReadRequest.RequestString("up");
        if (!string.IsNullOrEmpty(us) && !string.IsNullOrEmpty(us.Trim()))
        {
            Model.CMember member = BLL.CMember.userLogin(us.Trim(), BLL.Security.MD5(up));

            if (member != null && member.Id > 0)
            {
                System.Web.HttpContext.Current.Session[BLL.CMember.LoginUserSessionKey] = member;
                member.lastLoginTime = DateTime.Now;
                BLL.CMember.Update(member);
                returnmessage(true);
            }
            else
            {
                returnmessage(false);
                return;
            }
        }
        else
        {
            returnmessage(false);
            return;
        }
    }
    private void userloginout()
    {
        BLL.Session.Remove(BLL.CMember.LoginUserSessionKey);
    }
    private void find__pws()
    {
        string username = ReadRequest.RequestString("username");
        string email = ReadRequest.RequestString("email");
        username = username.TrimEnd(',');
        email = email.TrimEnd(',');
        if (!string.IsNullOrEmpty(username))
        {
            username = username.Trim();
        }
        else
        {
            alertmessageAndGoback("用户名不正确!");
            return;
        }
        if (string.IsNullOrEmpty(username) || username.Length < 2)
        {
            alertmessageAndGoback("用户名不正确!");
            return;
        }
        if (string.IsNullOrEmpty(email) || email.Length < 3 || email.Length > 30)
        {
            alertmessageAndGoback("邮箱不正确!");
            return;
        }
        Model.CMember model_GET = BLL.CMember.GetOneBynickname(username);
        if (model_GET != null && model_GET.nickname != null && model_GET.nickname.Length > 1 && username.ToLower().Equals(model_GET.nickname.ToLower()) && model_GET.email != null && model_GET.email.Length > 2 && email.ToLower().Equals(model_GET.email.ToLower()))
        {
            model_GET.u_password = BLL.Security.MD5("111111");
            if (BLL.CMember.Update(model_GET) > 0)
            {
                if (NtHelp.SendGetMobPassMail(model_GET.email, model_GET.nickname, "111111"))
                {
                    alertmessageAndGo("邮件已发送到您的邮箱，请查收！", "/default.aspx");
                    return;
                }
            }
        }
        alertmessageAndGoback("用户名或邮箱不正确!");
        return;
    }
    private void find_pws_mobel()
    {
        string username = ReadRequest.RequestHtmlEnCode("username");
        string userphone = ReadRequest.RequestHtmlEnCode("userphone");
        if (!string.IsNullOrEmpty(username))
        {
            username = username.Trim();
        }
        else
        {
            alertmessageAndGoback("用户名不可为空!");
            return;
        }
        if (string.IsNullOrEmpty(username) || username.Length < 2)
        {
            alertmessageAndGoback("用户名不正确!");
            return;
        }
        Model.CMember model_GET = BLL.CMember.GetOneBynickname(username);
        if (username.ToLower().Equals(model_GET.nickname.ToLower()) && userphone.ToLower().Equals(model_GET.mob.ToLower()))
        {
            model_GET.u_password = BLL.Security.MD5("111111");
            if (BLL.CMember.Update(model_GET) > 0)
            {
                string password = "111111";
                sdk.WebService sms = new sdk.WebService();
                Model.MsgSendInfo msgIngo = new Model.MsgSendInfo();
                string mobile = userphone;
                string content = "您好，您在米米彩票注册的用户名为" + username + ",密码重置为" + password + "！【米米彩票】";
                string sn = SendMsg.Uid;
                string passowrd = SendMsg.key;

                string url = "http://utf8.sms.webchinese.cn/?Uid=" + sn + "&Key=" + passowrd + "&smsMob=" + mobile + "&smsText=" + content;
                string returnMsg = SendMsg.GetHtmlFromUrl(url);
                if (returnMsg.Equals("-1"))
                {
                    alertmessageAndGoback("没有该用户账户");
                }
                else if (returnMsg.Equals("-2"))
                {
                    alertmessageAndGoback("接口密钥不正确");
                }
                else if (returnMsg.Equals("-21"))
                {
                    alertmessageAndGoback("MD5接口密钥加密不正确");
                }
                else if (returnMsg.Equals("-3"))
                {
                    alertmessageAndGoback("短信数量不足");
                }
                else if (returnMsg.Equals("-11"))
                {
                    alertmessageAndGoback("该用户被禁用");
                }
                else if (returnMsg.Equals("-14"))
                {
                    alertmessageAndGoback("短信内容出现非法字符");
                }
                else if (returnMsg.Equals("-4"))
                {
                    alertmessageAndGoback("手机号格式不正确");
                }
                else if (returnMsg.Equals("-41"))
                {
                    alertmessageAndGoback("手机号码为空");
                }
                else if (returnMsg.Equals("-42"))
                {
                    alertmessageAndGoback("短信内容为空");
                }
                else if (returnMsg.Equals("-51"))
                {
                    alertmessageAndGoback("短信签名格式不正确");
                }
                else if (returnMsg.Equals("-6"))
                {
                    alertmessageAndGoback("IP限制");
                }
                else
                {
                    msgIngo.msgType = 4;//1注册，2充值，3提现，4找回密码，5发布散标，
                    msgIngo.mobel = mobile;
                    msgIngo.contents = content;
                    msgIngo.addTime = DateTime.Now;
                    if (BLL.MsgSendInfo.Add(msgIngo) > 0)
                    {
                        alertmessageAndGo("密码已发送到您的手机，请查收！", "/Default.aspx");
                        return;
                    }

                }
            }
        }
        alertmessageAndGoback("用户名或电话号码不正确!");
        return;
    }
    private void ht_admin_login()
    {
        string username = ReadRequest.RequestHtmlEnCode("username");
        if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(username.Trim()))
        {
            Model.Manageadmin member = BLL.Manageadmin.GetOneBynickname(username.Trim());
            if (member != null && member.uesrState > 0)
            {
                if (member.u_password.Equals(BLL.Security.MD5(ReadRequest.RequestHtmlEnCode("userpass"))))
                {
                    System.Web.HttpContext.Current.Session[BLL.CMember.admin_SessionKey] = member;
                    try
                    {
                        member.lastLoginTime = DateTime.Now;
                        member.lastLoginIP = ReadRequest.getUserRealIP();
                        BLL.Manageadmin.Update(member);
                    }
                    catch { }
                    returnmessage(true);
                    return;
                }
            }
        }
        returnmessage(false);
        return;
    }
    private void admin_loginout()
    {
        BLL.Session.Remove(BLL.CMember.admin_SessionKey);
    }
    private void returnmessage(string message)
    {
        System.Web.HttpContext.Current.Response.Write(message);
    }
    private void returnmessage(bool value)
    {
        System.Web.HttpContext.Current.Response.Write(value.ToString());
    }
    private void alertmessage(string message, string cap)
    {
        System.Web.HttpContext.Current.Response.Write(cap + message + "!");
    }
    private void alertmessageAndGoback(string message)
    {
        System.Web.HttpContext.Current.Response.Write("<script type=\"text/javascript\">alert('" + message + "');history.go(-1);</script>");
    }
    private void alertmessageAndGo(string message, string url)
    {
        System.Web.HttpContext.Current.Response.Write("<script type=\"text/javascript\">alert('" + message + "');window.location.href = '" + url + "';</script>");
    }
    private void alertAndGo(string message, string url)
    {
        System.Web.HttpContext.Current.Response.Write("<script type=\"text/javascript\">alert('" + message + "');top.window.location.href = '" + url + "';</script>");
    }
    private void returnalert(string value)
    {
        System.Web.HttpContext.Current.Response.Write(value.ToString());
    }
}